//
//  CZWelComeViewController.swift
//  WHWeiBo
//
//  Created by Li Changjian on 16/7/12.
//  Copyright © 2016年 Li Changjian. All rights reserved.
//

import UIKit
import SDWebImage

class CZWelComeViewController: UIViewController {
/// 懒加载背景图
    lazy var backgroundView = UIImageView(image: UIImage(named: "ad_background"))
/// 懒加载用户头像
    lazy var iconImageView: UIImageView = {
        let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        if let imageUrl = CZUserAccountViewModel.sharedUserAccount.userAccount?.avatar_large {
            iconView.sd_setImageWithURL(NSURL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        iconView.layer.cornerRadius = 42.5
        iconView.layer.masksToBounds = true
        
        return iconView
    }()
/// 懒加载登录提示
    lazy var messageLabel: UILabel = {
        let label = UILabel()
        if let name = CZUserAccountViewModel.sharedUserAccount.userAccount?.name {
            label.text = "欢迎回来,\(name)"
        }else {
            label.text = "欢迎回来"
        }
        label.textColor = UIColor.darkGrayColor()
        label.alpha = 0
        return label
    }()
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.purpleColor()
        // Do any additional setup after loading the view.
 
        setupUI()
    }

    override func loadView() {
        view = backgroundView
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    // MARK: - setUpUI
    func setupUI() {
        
        view.addSubview(iconImageView)
        view.addSubview(messageLabel)
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(view).offset(275)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView.snp_bottom).offset(30)
            make.centerX.equalTo(iconImageView)
        }
    }
    // MARK: - 开始动画
    func startAnimation() {
        iconImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(125)
        }
        
        UIView.animateWithDuration(1, animations: { () -> Void in
            //更新约束
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        //发送通知,请求app切换根控制器
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewControllerNotification, object: nil)
                })
                
        }
    
    }
    
    

}
